package component.fy.com.common_module.base;

import android.app.Activity;

import java.util.Stack;

import component.fy.com.common_module.utils.LogUtil;

/**
 * Created by Administrator on 2017/5/11 0011.
 */

public class ActivityManager {
    private static Stack<Activity> aliveActivityStack;
    private static Stack<Activity> visibleActivityStack;
    private static ActivityManager instance;

    private ActivityManager() {
    }

    /**
     * 单一实例
     */
    public static ActivityManager getInstance() {
        if (instance == null) {
            instance = new ActivityManager();
            aliveActivityStack = new Stack<>();
            visibleActivityStack = new Stack<>();
        }
        return instance;
    }

    /**
     * 添加Activity到堆栈
     */
    public void addAliveActivity(Activity activity) {
        if (aliveActivityStack == null) {
            aliveActivityStack = new Stack<>();
        }
        aliveActivityStack.add(activity);
    }

    /**
     * 添加前台可见Activity到堆栈
     */
    public void addFrontActivity(Activity activity) {
        if (visibleActivityStack == null) {
            visibleActivityStack = new Stack<>();
        }
        visibleActivityStack.add(activity);
    }

    /**
     * 移出活动的堆栈
     * @param activity
     */
    public void removeAliveActivity(Activity activity) {
        if (aliveActivityStack == null) {
            aliveActivityStack = new Stack<>();
        }
        aliveActivityStack.remove(activity);
    }

    /**
     * 移出前端可见的堆栈
     * @param activity
     */
    public void removeFrontActivity(Activity activity) {
        if (visibleActivityStack == null) {
            visibleActivityStack = new Stack<>();
        }
        if (!visibleActivityStack.empty()) {
            visibleActivityStack.remove(activity);
        }
    }



    /**
     * 获取当前可见Activity，如果没有，获取当前还未销毁的Activity
     */
    public Activity currentVisibleActivity() {
        if (visibleActivityStack.size() > 0) {
            return visibleActivityStack.lastElement();
        }
        return currentAliveActivity();
    }

    /**
     * 获取当前Activity（堆栈中最后一个压入的）
     */
    public Activity currentAliveActivity() {
        if (aliveActivityStack.size() > 0) {
            return aliveActivityStack.lastElement();
        }
        return null;
    }

    /**
     * 结束当前Activity（堆栈中最后一个压入的）
     */
    public void finishActivity() {
        Activity activity = aliveActivityStack.lastElement();
        finishActivity(activity);
    }

    /**
     * 结束指定的Activity
     */
    private void finishActivity(Activity activity) {
        if (activity != null) {
            aliveActivityStack.remove(activity);
            activity.finish();
            activity = null;
        }
    }

    /**
     * 结束指定类名的Activity
     */
    public void finishActivity(Class<?> cls) {
        for (Activity activity : aliveActivityStack) {
            if (activity.getClass().equals(cls)) {
                finishActivity(activity);
            }
        }
    }

    /**
     * 结束所有Activity
     */
    public void finishAllActivity() {
//        for (int i = 0, size = aliveActivityStack.size(); i < size; i++) {
//            if (null != aliveActivityStack.get(i)) {
//                aliveActivityStack.get(i).finish();
//            }
//        }
        while (!aliveActivityStack.empty()) {
            if (null != aliveActivityStack.get(0)) {
                aliveActivityStack.get(0).finish();
                aliveActivityStack.remove(0);
            }
        }
//        aliveActivityStack.clear();
    }
}
